<template>
	<view class="register">

		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="用户名/电话"></wInput>
				<wInput v-model="passData1" type="password" maxlength="11" placeholder="登录密码" isShowPass></wInput>
				<wInput v-model="passData2" type="password" maxlength="11" placeholder="再次输入密码" isShowPass></wInput>

			</view>

			<wButton class="wbutton" text="注 册" :rotate="isRotate" @click.native="startReg()"></wButton>

			<!-- 底部信息 -->
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon" :class="showAgree?'cuIcon-radiobox':'cuIcon-round'"> 同意
				</text>
				<!-- 协议地址 -->
				<navigator url="" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import req from '../../config/request.js'

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', // 用户名电话
				passData1: '', //密码
				passData2: '', //密码
				verCode: "", //验证码
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		methods: {
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			startReg() {
				//注册
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请先同意《协议》'
					});
					return false;
				}
				if (this.phoneData.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名或电话号码不正确'
					});
					return false;
				}
				if (this.passData1.length <= 0) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请输入登录密码'
					});
					return false;
				}
				if (this.passData2.length <= 0) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请再次输入登录密码'
					});
					return false;
				}
				if (this.passData1.length < 6 || this.passData2.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码长度需要大于6'
					});
					return false;
				}


				if (this.passData1 != this.passData2) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '两次输入密码不一致'
					});
					return false;
				}


				uni.request({
					url: req.url + '/register',
					method: 'POST',
					data: {
						uid: this.phoneData,
						password: this.passData1
					},
					success: (res) => {

						if (res.data == 'ok') {
							uni.showToast({
								icon: 'none',
								position: 'bottom',
								title: '注册成功'
							});
							
							setTimeout(function() {
								uni.navigateTo({
									url: '../login/login'
								})
							}, 4000)
							
						} else{
							
							setTimeout(function() {
								uni.showToast({
									icon: 'none',
									position: 'bottom',
									title: '该用户已存在'
								});
							}, 4000)
							
						}
					}
				});
				
				console.log("注册成功")
				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
